import Common from '../common'
import region from '../../utils/cityTencent.json'
var Js = Common('region')
export default {
  name: 'region',
  mixins: [Js],
  components: {},
  data() {
    return {
      fields: [{
          key: 'id',
          label: 'ID',
          application: [],
          componentName: 'sls-input',
          listType: '',
          width: 50
        },
        {
          key: "region",
          label: '区域',
          application: ['编辑', '添加'],
          componentName: 'sls-cascader',
          listType: 'normal',
          optionsName: 'regionOptions',
          defaultProps: {
            children: 'children',
            label: 'fullname',
            value: 'fullname',
            array: true
          },
          changeFunc: function(val, self) {
            self.submitData.region = val.length > 0 ? val.join(',') : ''
          },
          formatter: function(val) {
            return val.region.join('/')
          }
        },
        {
          key: 'map',
          label: '经纬度',
          application: ['编辑', '添加'],
          componentName: 'tencent-map',
          listType: '',
          dialogStyle: 'width:100%',
          changeFunc: function(value, self) {
            self.submitData.longitude = value['lng'];
            self.submitData.latitude = value['lat'];
            self.formData.longitude = value['lng'];
            self.formData.latitude = value['lat'];
          },
        },
        {
          key: 'longitude',
          label: '经度',
          application: ['编辑', '添加'],
          componentName: 'sls-input',
          listType: '',
        },
        {
          key: 'latitude',
          label: '纬度',
          application: ['编辑', '添加'],
          componentName: 'sls-input',
          listType: '',
        },
        {
          key: 'create_time',
          label: '创建时间',
          listType: 'normal',
          placeholder: '请选择创建时间',
          header_search: {
            componentName: 'sls-date-time-range',
            style: 'width:160px;margin-right:2px;',
            start_placeholder: '创建开始时间',
            end_placeholder: '创建结束时间',
            changeFunc: function(value, self) {
              if (!value) {
                delete self.searchItem.create_time;
              } else {
                self.searchItem.create_time = ['between', value = value.map(function(e) {
                  return e / 1000;
                })]
              };
            },
          },
          width: 200,
        },
        {
          label: '操作',
          listType: 'deal',
          width: 120
        },
      ],

      // 按钮配置
      btn_info: [{
          type: '',
          icon: 'el-icon-edit-outline',
          size: 'mini',
          position: 'list',
          text: function(data) {
            return '编辑'
          },
          func: {
            apiName: function(self) {
              return "api_articleUpdate"
            },
            formData: function(self) {
              return self.formData
            },
            postData: function(self) {
              var postData = {
                searchItem: {
                  id: self.formData.id,
                },
                data: self.submitData
              };
              if (self.submitData.parentid && self.submitData.parentid == self.formData.id) {
                self.$$notify('父级ID和子级ID重叠', 'fail');
                return false;
              } else {
                return postData;
              };
            }
          },
        },
        {
          type: 'danger',
          icon: 'delete',
          size: 'mini',
          funcType: 'submit',
          position: 'header',
          text: function(data) {
            return '删除选中'
          },
          func: {
            apiName: function(self) {
              return "api_articleUpdate"
            },
            postData: function(self) {
              var deleteArray = [];
              for (var i = 0; i < self.selectionArray.length; i++) {
                deleteArray.push(self.selectionArray[i].id);
              };
              var postData = {
                searchItem: {
                  id: ['in', deleteArray],
                },
                data: {
                  status: -1
                }
              };
              return postData;
            }
          },
        },
        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          position: 'header',
          text: function(data) {
            return '添加'
          },
          func: {
            apiName: function(self) {
              return "api_articleAdd"
            },
            formData: function(self) {
              var data = {
                title: '',
                longitude: '',
                latitude: '',
                region: []
              };
              return data
            },
            postData: function(self) {
              self.submitData.menu_id = 4;
              var postData = {
                data: self.submitData
              };
              return postData;
            }
          },
        },

      ],
      searchItem: {
        menu_id: 4
      },
      startSearchItem: {
        menu_id: 4
      },
      optionData: {
        regionOptions: [],
        statusOptions: [{
          text: '启用',
          value: 1
        }, {
          text: '禁用',
          value: -1
        }],
      },
      otherData: {},
      UserInfo: {
        tableName: 'UserInfo',
        searchItem: {},
        key: 'user_no',
        middleKey: 'user_no',
        condition: 'in',
      }

    }

  },

  mounted() {
    const self = this;
    self.optionData.regionOptions = region;
    this.init()
  },
  computed: {
    token: function() {
      return this.$store.getters.getToken
    }
  },
  methods: {

    /**
     * 初始化
     */
    init() {
      this.initMainData();

    },


    /**
     * 列表主函数
     */
    async initMainData(isNew) {

      const self = this;
      self.table_arguments.loading = true;
      const postData = {};
      if (isNew) {
        self.paginate.currentPage = 1;
      };
      postData.paginate = self.$$cloneForm(self.paginate);
      postData.token = self.$store.getters.getToken;
      if (self.searchItem) {
        postData.searchItem = self.$$cloneForm(self.searchItem)
      };
      if (JSON.stringify(self.getBefore) != "{}") {
        postData.getBefore = self.$$cloneForm(self.getBefore);
      };
      var res = await self.$$api_articleGet({
        data: postData
      });
      for (var i = 0; i < res.info.data.length; i++) {
        res.info.data[i].region = res.info.data[i].region?res.info.data[i].region.split(','):[]
      }
      self.mainData = res.info.data;
      self.contentData = self.mainData[0];
      self.paginate.count = res.info.total;
      self.table_arguments.loading = false;

    },

    async initMenuData() {
      const self = this;
      const postData = {};
      postData.searchItem = {
        status: 1,
      };
      postData.token = self.$store.getters.getToken;
      postData.order = {
        listorder: 'desc'
      };

      try {
        var res = await self.$$api_labelGet({
          data: postData
        });
      } catch (err) {
        console.log(err);
        self.$notify('网络故障', 'error');
      };

      if (res) {
        self.optionData.regionOptions = res.info.data;
        console.log('self.optionData.regionOptions', self.optionData.regionOptions)
        var idArray = []
        for (var i = 0; i < self.optionData.regionOptions.length; i++) {
          idArray.push(self.optionData.regionOptions[i].id)
        }
        console.log('idArray', idArray)

        self.searchItem.menu_id = ['in', idArray];
        // self.searchItem.menu_id = self.optionData.regionOptions[0].id;
        // self.fields[5]['header_search']['defaultValue'] = self.searchItem.menu_id;
        // self.$set(self.fields,5,self.fields[5]);
        self.initMainData();
      };


    },



  },


}
